#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define lowbit(x) x&(-x)
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL tree[maxn];
queue<LL>v[30];
LL num[maxn];
char s1[maxn],s2[maxn];
LL n;
void add(LL x,LL d){
    while(x<maxn){
        tree[x]+=d;
        x+=lowbit(x);
    }
}
LL getsum(LL x){
   LL sum=0;
   while(x>0){
      sum+=tree[x];
      x-=lowbit(x);
   }
   return sum;
}
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL n;cin>>n;
   cin>>(s1+1);
   for(LL i=1;i<=n;i++){
       v[s1[i]-'a'+1].push(i);
   }
   for(LL i=1;i<=n;i++){
       s2[i]=s1[n-i+1];
   }
   for(LL i=1;i<=n;i++){
       num[i]=v[s2[i]-'a'+1].front();
       v[s2[i]-'a'+1].pop();
   }
   LL ans=0;
   for(LL i=1;i<=n;i++){
      add(num[i],1);
      ans+=(i-getsum(num[i]));
   }
   cout<<ans<<"\n";
   return 0;
}
